/**
 * Follow
 */
define('components/Follow/Follow', ['utils/fetchEnhancer'], function(fetch){
function F(toFollowUid) {
    this.followed = true;
    this.toFollowUid = toFollowUid;

    this.button = null;

    this.init();
    this.bindEvent();
}
F.prototype = {
    constructor: F,
    init: function() {
        this.button = document.createElement('button');
        this.button.setAttribute('type', 'button');
        this.button.className = 'x-btn';
    },
    bindEvent: function() {
        var that = this;
        this.button.onclick = function() {
            // login check
            if(!USER_SDK.getInstance().isLogin()) {
                USER_SDK.getInstance().showLogin();
                return;
            }

            var api = that.followed ? '/api/fans/unfollow' : '/api/fans/follow';
            fetch(api, {
                method: 'POST',
                body: 'uid=' + that.toFollowUid
            }).then((data) => {
                if(0 === data.status) {
                    that.followed = !that.followed;
                    that.resetText();
                }
            });
        };
    },
    resetText: function() {
        this.button.innerHTML = this.followed ? '已关注' : '关注';
    },
    render: function(container) {
        var that = this;

        fetch('/api/fans/followed', {
            method: 'POST',
            body: 'uid=' + this.toFollowUid

        }).then(function(res){
            // 不能关注自己
            if(100011 === res.status) {
                return;
            }

            if(0 === res.status) {
                that.followed = res.data.followed;

            } else {
                that.followed = false;
            }

            that.resetText();
            container.appendChild(that.button);
        });
    }
};

return F;
});
